import { TaroBLEUtil } from "./taroBLEUtil";
import { WebBLEUtil } from "./webBLEUtil";

export interface BLEUtilOptions {
    serviceId?: string[];
    deviceName?: string;
    deviceId?: string;
    platform: "WEB" | "UNI_APP" | "TARO"
}

export interface IBLEUtilImpl {
    /**链接 */
    connect: () => Promise<boolean>;
    /**关闭连接 */
    close(): Promise<void>;
    /**设置监听 */
    addEventListener: (serviceId: string, uuid: string[], callBack: (values: { uuid: string, values: number[], rawData: any }) => void) => Promise<void>;
}




export default class BLEUtil {

    public static getInstance(options: BLEUtilOptions): IBLEUtilImpl {
        const { platform } = options;
        if (platform === "WEB") {
            if (!window.navigator) {
                throw new Error("window.navigator 不存在 请在https环境中使用或者开发的时候使用localhost环境使用 或者检查浏览器是否支持蓝牙模块");
            }
            if (!options.serviceId) {
                throw new Error("浏览器环境中serviceId不能为空")
            }
            return new WebBLEUtil(options.serviceId);
        }
        else if (platform === "TARO") {
            return new TaroBLEUtil();
        }
        return new WebBLEUtil(options.serviceId!);
    };

    // deivce!: BluetoothDevice;
    // service!: BluetoothRemoteGATTService;
    // gatt!: BluetoothRemoteGATTServer;

    // public constructor(deivce: BluetoothDevice, service: BluetoothRemoteGATTService, gatt: BluetoothRemoteGATTServer) {
    //     this.deivce = deivce;
    //     this.service = service;
    //     this.gatt = gatt;
    // }

    // public static async getInstance(serviceId: string) {
    //     let device = await navigator.bluetooth.requestDevice({
    //         optionalServices: [serviceId],
    //         acceptAllDevices: true
    //     });
    //     let gatt = await device.gatt?.connect()!;
    //     let server = await gatt.getPrimaryService(serviceId);
    //     return new BLEUtil(device, server, gatt);
    // }

};